package com.zimi.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zimi.mapper.SysOrgExtendMapper;
import com.zimi.model.SysOrgExtend;
import com.zimi.model.WfRoleUser;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * api
 *
 * @author lnintr
 * @since 2019-07-11
 */
@RestController
@RequestMapping("wf-role-user")
public class WfRoleUserApi {

    @Autowired
    private SysOrgExtendMapper sysOrgExtendMapper;

    @PostMapping("updateWfRoleUser")
    @ApiOperation("配置流程角色对应的人员")
    @ResponseBody
    public Object updateWfRoleUser(Integer wfRoleId, String strIds) {
        Map map = new HashMap();

        new WfRoleUser().delete(new QueryWrapper<WfRoleUser>().lambda().eq(WfRoleUser::getWfRoleId, wfRoleId));
        strIds = strIds.replaceAll("\\[", "").replaceAll("\\]", "");

        if (!strIds.equals("")) {
            String[] userIdList = null;
            userIdList = strIds.split(",");

            WfRoleUser saveData = new WfRoleUser();
            saveData.setWfRoleId(wfRoleId);

            for (String userId : userIdList) {
                saveData.setUserId(Integer.parseInt(userId));
                saveData.insert();
            }
        }

        map.put("OK", "OK");
        map.put("code", 200);

        return map;
    }

    @GetMapping("selectList")
    @ApiOperation("查询所有")
    @ResponseBody
    public Object selectList(WfRoleUser record) {
        Map map = new HashMap();
        List<SysOrgExtend> userList = new ArrayList<>();
        List<Integer> ids = new ArrayList<>();

        if (record.getWfRoleId() != null) {
            List<WfRoleUser> listRoleUser = record.selectList(new QueryWrapper<WfRoleUser>().setEntity(record));
            if (listRoleUser.size() > 0) {
                for (WfRoleUser iRoleUser : listRoleUser) {
                    if (ids.indexOf(iRoleUser.getUserId()) == -1) {
                        ids.add(iRoleUser.getUserId());
                    }
                }
                userList = sysOrgExtendMapper.selectUserListByIds(ids);
            }
        }

        map.put("List", userList);
        map.put("OK", "OK");
        map.put("code", 200);

        return map;
    }
}
